//npm run build打包前执行此段代码
let fs = require('fs');

//返回package的json数据
function getPackageJson() {
  let data = fs.readFileSync('./package.json');//fs读取文件
  return JSON.parse(data);//转换为json对象
}
var nowDate = new Date();
var date = {
year: nowDate.getFullYear(),
month: nowDate.getMonth() + 1,
day: nowDate.getDate(),
}
var year = date.year
var month = (date.month >= 10 ? date.month : '0' + date.month)
var day = (date.day >= 10 ? date.day : '0' + date.day)

let packageData = getPackageJson();//获取package的json
let arr = packageData.version.split('.');//切割后的版本号数组
if(arr[0]==year && arr[1]==month && arr[2]==day){
    arr[3] = parseInt(arr[3]) + 1;
}else{
    arr[0] = year;
    arr[1] = month;
    arr[2] = day;
    arr[3] = 1;
}

packageData.version = arr.join('.');//转换为以"."分割的字符串
//用packageData覆盖package.json内容
fs.writeFile(
  './package.json',
  JSON.stringify(packageData, null, "\t"
  ),
  (err) => { }
);